package io.adrainty.bolee.framework.commons.utils;

import io.adrainty.bolee.framework.commons.basic.IEnum;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;

/**
 * <p>EnumUtil</p>
 *
 * @author AdRainty
 * @version V1.0.0
 * @description EnumUtil
 * @since 2025/8/5 13:22:33
 */
public class EnumUtil {

    private EnumUtil() {}

    public static <E extends IEnum> E getEnumConstants(String code, Class<E> enumClass) {
        if (StringUtils.isBlank(code)) {
            throw new IllegalArgumentException("Code cannot empty");
        }
        E[] enumConstants = enumClass.getEnumConstants();
        return Arrays.stream(enumConstants).filter(e -> code.equals(e.getCode()))
                .findFirst()
                .orElseThrow(() -> new IllegalArgumentException("Code " + code + "is not exists in " + enumClass.getName()));
    }

}
